/********************************************************************************
PORTB4~7
PORTE3~6    -   PWM PIN  :  relay

POERTD0~8   -   LED 
*********************************************************************************/
#include <iom128.h>
#include <ina90.h>
#include "HK_init.h"
#include "HK_uart.h"
#include "HK_timer.h"

unsigned char rx = 0;

void relay_LED1_on();
void relay_LED2_on();
void relay_LED3_on();
void relay_LED4_on();
void relay_LED5_on();
void relay_LED6_on();
void relay_LED7_on();
void relay_LED8_on();


void relay_LED1_off();
void relay_LED2_off();
void relay_LED3_off();
void relay_LED4_off();
void relay_LED5_off();
void relay_LED6_off();
void relay_LED7_off();
void relay_LED8_off();


void LED_TEST();
void Fountain();

int main( void )
{
    Port_Initialization();
    Usart_Initialization();
    //Timer_Initialization();
    
    __enable_interrupt(); 
         
    while(1)
    {
       
      LED_TEST();      
      //Fountain();
    }
    
    
}

void Fountain()
{
  
     if(((rx>>1)&&0x01)==1)
      {
          relay_LED1_on();
      }
      if(((rx>>2)&&0x01)==1)
      {
          relay_LED2_on();
      }
      if(((rx>>3)&&0x01)==1)
      {
          relay_LED3_on();
      }
      if(((rx>>4)&&0x01)==1)
      {
          relay_LED4_on();
      }
      if(((rx>>5)&&0x01)==1)
      {
          relay_LED5_on();
      }
       if(((rx>>6)&&0x01)==1)
      {
        relay_LED6_on();
      }
      if(((rx>>7)&&0x01)==1)
      {
          relay_LED7_on();
      }
 
  
}

void LED_TEST()
{
  
      relay_LED1_on();
      delay_ms(50);
      relay_LED2_on();
      delay_ms(50);
      relay_LED3_on();
      delay_ms(50);
      relay_LED4_on();
      delay_ms(50);
      relay_LED5_on();
      delay_ms(50); 
      relay_LED6_on();
      delay_ms(50); 
      relay_LED7_on();
      delay_ms(50); 
      relay_LED8_on();
      delay_ms(50); 
      
      relay_LED8_off();
      delay_ms(50);
      relay_LED7_off();
      delay_ms(50);
      relay_LED6_off();
      delay_ms(50);
      relay_LED5_off();
      delay_ms(50);
      relay_LED4_off();
      delay_ms(50); 
      relay_LED3_off();
      delay_ms(50); 
      relay_LED2_off();
      delay_ms(50); 
      relay_LED1_off();
      delay_ms(50); 
}

/////////////////////////////////////////////

void relay_LED1_on()
{
    PORTB_Bit4 = 1;
    PORTD_Bit0 = 1;
  
}
void relay_LED2_on()
{
    PORTB_Bit5 = 1; 
    PORTD_Bit1 = 1;
  
}
void relay_LED3_on()
{
    PORTB_Bit6 = 1;
    PORTD_Bit2 = 1;
  
}
void relay_LED4_on()
{
    PORTB_Bit7 = 1;
    PORTD_Bit3 = 1;
  
}
void relay_LED5_on()
{
    PORTE_Bit3 = 1;
    PORTD_Bit4 = 1;
  
}
void relay_LED6_on()
{
    PORTE_Bit4 = 1; 
    PORTD_Bit5 = 1;
}
void relay_LED7_on()
{
    PORTE_Bit5 = 1; 
    PORTD_Bit6 = 1;
  
}
void relay_LED8_on()
{
    PORTE_Bit6 = 1;    
    PORTD_Bit7 = 1;  
  
}


//////////////////////////////////////////////////////////

void relay_LED1_off()
{
    PORTB_Bit4 = 0;
    PORTD_Bit0 = 0;
  
}
void relay_LED2_off()
{
    PORTB_Bit5 = 0; 
    PORTD_Bit1 = 0;
  
}
void relay_LED3_off()
{
    PORTB_Bit6 = 0;
    PORTD_Bit2 = 0;
  
}
void relay_LED4_off()
{
    PORTB_Bit7 = 0;
    PORTD_Bit3 = 0;
  
}
void relay_LED5_off()
{
    PORTE_Bit3 = 0;
    PORTD_Bit4 = 0;
  
}
void relay_LED6_off()
{
    PORTE_Bit4 = 0; 
    PORTD_Bit5 = 0;
}
void relay_LED7_off()
{
    PORTE_Bit5 = 0; 
    PORTD_Bit6 = 0;
  
}
void relay_LED8_off()
{
    PORTE_Bit6 = 0;    
    PORTD_Bit7 = 0;  
  
}
/////////////////////////////////////////////////////

#pragma vector=USART0_RXC_vect
__interrupt void USART0_RXC_interrupt(void){
  
    rx = UDR0;     
   
}
